﻿#include "FITKAbaLoadMoment.h"
#include "FITK_Kernel/FITKCore/FITKDataRepo.h"
#include "FITK_Interface/FITKInterfaceModel/FITKCoordinateSystem.h"

Interface::FITKAbaAbstractLoad::FITKAbaLoadType Interface::FITKAbaLoadMoment::getLoadType() const
{
    return Interface::FITKAbaAbstractLoad::FITKAbaLoadType::Moment;
}

bool Interface::FITKAbaLoadMoment::isModifiedInStep(int stepId) const
{
    //判断载荷在分析步中是否被修改的状态
    if (m_modifiedDatas.contains(stepId))
        return true;
    return false;
}

void Interface::FITKAbaLoadMoment::activeStateChanged()
{
    //当激活状态发生变化时，对载荷数据进行调整,激活时不做任何处理
    if (m_deactiveStepId == -1)
        return;
    //取消激活时，将取消激活的分析步及后续分析步中的数据删除
    //访问当前算例的分析步管理器，获取分析步顺序id列表。
    QVector<int> stepIds = this->getStepIds();
    int index = stepIds.indexOf(m_deactiveStepId);
    if (index == -1)
        return;
    for (int i = index ; i < stepIds.count(); ++i)
    {
        int id = stepIds[i];
        if (m_modifiedDatas.contains(id))
            m_modifiedDatas.remove(id);
    }
}

bool Interface::FITKAbaLoadMoment::copy(FITKAbstractDataObject *obj)
{
    bool ok = FITKAbaAbstractLoad::copy(obj);
    if (!ok)
        return false;
    FITKAbaLoadMoment* force = dynamic_cast<FITKAbaLoadMoment*>(obj);
    if (force == nullptr)
        return false;
    this->setDataObjectName(force->getDataObjectName());
    this->m_data = force->m_data;
    this->m_modifiedDatas = force->m_modifiedDatas;
    this->m_csysId = force->m_csysId;
    this->m_analyticalFieldId = force->m_analyticalFieldId;
    return true;
}

double Interface::FITKAbaLoadMoment::getCM1(int stepId)
{
    return this->getDataInStep(stepId).getValue1();
}

void Interface::FITKAbaLoadMoment::setCM1(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setValue1(value);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setValue1(value);
    }
}

double Interface::FITKAbaLoadMoment::getCM2(int stepId)
{
    return this->getDataInStep(stepId).getValue2();
}

void Interface::FITKAbaLoadMoment::setCM2(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setValue2(value);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setValue2(value);
    }
}

double Interface::FITKAbaLoadMoment::getCM3(int stepId)
{
    return this->getDataInStep(stepId).getValue3();
}

void Interface::FITKAbaLoadMoment::setCM3(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setValue3(value);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setValue3(value);
    }
}

int Interface::FITKAbaLoadMoment::getAmplitudeId(int stepId)
{
    return this->getDataInStep(stepId).getAmplitudeId();
}

void Interface::FITKAbaLoadMoment::setAmplitudeId(int id, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setAmplitudeId(id);
    else
    {
        if (! m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setAmplitudeId(id);
    }
}

int Interface::FITKAbaLoadMoment::getAnalyticalFieldId() const
{
    return m_analyticalFieldId;
}

void Interface::FITKAbaLoadMoment::setAnalyticalFieldId(int id)
{
    m_analyticalFieldId = id;
}

int Interface::FITKAbaLoadMoment::getCsysId() const
{
    return m_csysId;
}

void Interface::FITKAbaLoadMoment::setCsysId(int id)
{
    m_csysId = id;
}

Interface::FITKCoordinateSystem* Interface::FITKAbaLoadMoment::getCsysObj()
{
    if (m_csysId < 0) return nullptr;
    return FITKDATAREPO->getTDataByID<FITKCoordinateSystem>(m_csysId);
}

Interface::ConcentratedLoadData Interface::FITKAbaLoadMoment::getDataInStep(int stepId)
{
    if (stepId <= 0)
        return m_data;
    if (m_modifiedDatas.contains(stepId))
        return m_modifiedDatas[stepId];
    QVector<int> stepIds = this->getStepIds();
    int createdStepIndex = stepIds.indexOf(m_createdStepId);
    int stepIndex = stepIds.indexOf(stepId);
    if (!(createdStepIndex >= 0 && createdStepIndex < stepIds.size()))
        return m_data;
    if (!(stepIndex >= 0 && stepIndex < stepIds.size()))
        return m_data;
    for (int i = stepIndex; i >= createdStepIndex; --i)
    {
        int id = stepIds.at(i);
        if (m_modifiedDatas.contains(id))
            return m_modifiedDatas[id];
    }
    return m_data;
}
